<?php

/**
 * @Project NUKEVIET 3.x - GETNEWS
 * @Author ĐẶNG ĐÌNH TỨ (dlinhvan@gmail.com)
 * @Copyright (C) 2010 webdep24.com All rights reserved
 * @Createdate 10/08/2012 08:00
 */

if( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' ) or ! defined( 'NV_IS_MODADMIN' ) ) die( 'Stop!!!' );

$submenu['zing'] = $lang_module['zing'];
$submenu['vnex'] = $lang_module['vnex'];
$submenu['dt'] = $lang_module['dt'];
$submenu['24h'] = $lang_module['h24h'];
$submenu['ngoisao'] = $lang_module['ngoisao'];

$submenu['setting'] = $lang_module['setting'];
$allow_func = array(
	'main',
	'zing',
	'vnex',
	'ngoisao',
	'dt',
	'24h',
	'test',
	'setting',
	'del',
	'add',
	'act',
	'update' );

define( 'NV_IS_FILE_ADMIN', true );

require_once ( NV_ROOTDIR . "/modules/" . $module_name . "/global.function.php" );
require_once ( NV_ROOTDIR . "/modules/" . $module_name . "/simple_html_dom.php" );

require_once ( NV_ROOTDIR . "/includes/class/geturl.class.php" );

function nguontin( $source, $url )
{
	global $db, $module_config, $module_name;
	$module = $module_config[$module_name]['module'];
	$mod_data = str_replace( '-', '_', $module );
	if( $source == 0 )
	{
		$url_info = @parse_url( $url );
		if( isset( $url_info['scheme'] ) and isset( $url_info['host'] ) )
		{
			$sourceid_link = $url_info['scheme'] . "://" . $url_info['host'];
			list( $source ) = $db->sql_fetchrow( $db->sql_query( "SELECT `sourceid` FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_sources` WHERE `link`=" . $db->dbescape( $sourceid_link ) ) );
			if( empty( $source ) )
			{
				list( $weight ) = $db->sql_fetchrow( $db->sql_query( "SELECT max(`weight`) FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_sources`" ) );
				$weight = intval( $weight ) + 1;
				$query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $mod_data . "_sources` (`sourceid`, `title`, `link`, `logo`, `weight`, `add_time`, `edit_time`) VALUES (NULL, " . $db->dbescape( str_replace( 'www.', '', $url_info['host'] ) ) . ", " . $db->dbescape( $sourceid_link ) . ", '', " . $db->dbescape( $weight ) . ", UNIX_TIMESTAMP( ), UNIX_TIMESTAMP( ))";
				$source = $db->sql_query_insert_id( $query );
			}
		}
	}
	return true;
}

function xuat_html( $ac )
{
	if( $ac == 1 )
	{
		$html = "
		<table width='100%' border='1' bgcolor='#999999' style='border:1px #FFF solid'>
		  <tr>
			<td width='5%' height=28 align='center' style='color:#fff;border:1px #FFF solid'><strong>STT</strong></td>
			<td width='61%' align='center' style='color:#fff;border:1px #FFF solid'><strong>Tiêu Đề Bài Viết</strong></td>
			<td width='34%' align='center' style='color:#fff;border:1px #FFF solid'><strong>Thời Gian Đăng</strong></td>
		  </tr>
		</table>
		";
	}
	else
	{
		$html = "
		<script type='text/javascript'> 
			function ajax_request() {
			  $('#dangdinhtu').html('<div id=\"loading\" style=\"color:#EB8B03; background:#000 ;border-radius: 10px;border:3px solid #EB8B03;padding:20px; \"><br><b>Hệ thống đang tải dữ liệu,vui lòng chờ trong giây lát.</b></div>'); 
			}
		</script>
		";
	}
	return $html;
}

function nv_news_fix_block( $bid, $repairtable = true )
{
	global $db, $db_config, $module_name, $op, $module_config;
	$module = $module_config[$module_name]['module'];
	$mod_data = str_replace( '-', '_', $module );
	$bid = intval( $bid );
	if( $bid > 0 )
	{
		$query = "SELECT `id` FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_block` where `bid`='" . $bid . "' ORDER BY `weight` ASC";
		$result = $db->sql_query( $query );
		$weight = 0;
		while( $row = $db->sql_fetchrow( $result ) )
		{
			$weight++;
			if( $weight <= 100 )
			{
				$sql = "UPDATE `" . NV_PREFIXLANG . "_" . $mod_data . "_block` SET `weight`=" . $weight . " WHERE `bid`='" . $bid . "' AND `id`=" . intval( $row['id'] );
			}
			else
			{
				$sql = "DELETE FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_block` WHERE `bid`='" . $bid . "' AND `id`=" . intval( $row['id'] );
			}
			$db->sql_query( $sql );
		}
		$db->sql_freeresult();
		if( $repairtable )
		{
			$db->sql_query( "REPAIR TABLE `" . NV_PREFIXLANG . "_" . $mod_data . "_block`" );
			$db->sql_query( "OPTIMIZE TABLE `" . NV_PREFIXLANG . "_" . $mod_data . "_block`" );
		}
	}
}

function nv_get_cat( $module, $hauto, $url, $sourceid, $catids )
{
	global $db, $module_data, $array_block_cat_module;
	$xuat = "";
	$select = "";
	$xuat .= "
	<form action=\"\" enctype=\"multipart/form-data\" method=\"post\" style=\"width:90%\" >
	<input type=\"hidden\" name=\"do\" value=\"1\" />";

	$sql1 = "SELECT id, title, link FROM `" . NV_PREFIXLANG . "_" . $module_data . "_" . $hauto . "`";
	$result = $db->sql_query( $sql1 );
	$xuat .= "
	<strong>Chuyên Mục Lấy Tin</strong>
	<select style=\"width:140px\" name=\"cat\">";
	while( $row = $db->sql_fetchrow( $result ) )
	{
		$select = ( $url == $row['link'] ) ? " selected=\"selected\"" : "";
		$xuat .= "<option value=\"" . $row['link'] . "\" " . $select . ">" . $row['title'] . "</option>";
	}
	$xuat .= "</select>";
	$mod_data = str_replace( '-', '_', $module );
	$xuat .= "<strong>Chuyện Mục Lưu Tin</strong><select style=\"width:140px\" name=\"catids\">";
	$array_catid_module = array();
	$sql = "SELECT catid, title, lev FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_cat` ORDER BY `order` ASC";
	$result_cat = $db->sql_query( $sql );
	while( list( $catid_i, $title_i, $lev_i ) = $db->sql_fetchrow( $result_cat ) )
	{
		$array_catid_module[] = array(
			"catid" => $catid_i,
			"title" => $title_i,
			"lev" => $lev_i );
	}
	$rowcontent['listcatid'] = "";
	$array_catid_in_row = explode( ",", $rowcontent['listcatid'] );
	foreach( $array_catid_module as $value )
	{
		$xtitle_i = "";
		if( $value['lev'] > 0 )
		{
			for( $i = 1; $i <= $value['lev']; $i++ )
			{
				$xtitle_i .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			}
		}
		$array_temp = array();
		$array_temp['value'] = $value['catid'];
		$array_temp['title'] = $xtitle_i . $value['title'];
		$select = ( $catids == $array_temp['value'] ) ? " selected=\"selected\"" : "";
		$xuat .= "<option value=\"" . $array_temp['value'] . "\" " . $select . ">" . $array_temp['title'] . "</option>";
	}

	$xuat .= "</select>";
	// nguồn tin
	$sql2 = "SELECT sourceid, title FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_sources`";
	$result = $db->sql_query( $sql2 );
	$xuat .= "
	<strong>Nguồn Tin</strong>
	<select  style=\"width:140px\" name=\"sourceid\">";
	$xuat .= "<option value=\"0\">Chọn nguồn tin</option>";
	while( $row = $db->sql_fetchrow( $result ) )
	{
		$select = ( $sourceid == $row['sourceid'] ) ? " selected=\"selected\"" : "";

		$xuat .= "<option value=\"" . $row['sourceid'] . "\" " . $select . ">" . $row['title'] . "</option>";
	}
	$xuat .= "</select>";
	///////////
	$array_block_cat_module = array();
	$id_block_content = array();
	$sql = "SELECT bid, adddefault, title FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_block_cat` ORDER BY `weight` ASC";
	$result = $db->sql_query( $sql );
	while( list( $bid_i, $adddefault_i, $title_i ) = $db->sql_fetchrow( $result ) )
	{
		$array_block_cat_module[$bid_i] = $title_i;
		if( $adddefault_i )
		{
			$id_block_content[] = $bid_i;
		}
	}
	if( count( $array_block_cat_module ) > 0 )
	{
		$xuat .= "<table><tr><td height=40><strong>Bài viết thuộc các nhóm tin:</strong>";
		foreach( $array_block_cat_module as $bid_i => $bid_title )
		{
			$ch = in_array( $bid_i, $id_block_content ) ? " checked=\"checked\"" : "";
			$xuat .= "<input class=\"news_checkbox\" type=\"checkbox\" name=\"bids[]\" value=\"" . $bid_i . "\"" . $ch . " />" . $bid_title . "&nbsp;\n";
		}
		$xuat .= "</td></tr><table/>";
	}

	////////////////
	$xuat .= "<input type=\"submit\" id=\"submit\" onclick=\"ajax_request()\" value=\"Cập Nhật\"/></form>";
	$xuat .= "<div id=dangdinhtu style=\"position:fixed;left:50%;top:50%; margin-left:-170px;margin-top:-75px;z-index: 1\" align=\"center\"></div>";
	return $xuat;
}

function nv_save_data( $module, $catids, $sourceid, $title, $alias, $hometext, $publtime, $homefile, $homeimgthumb, $bodyhtml, $link, $id_block_content )
{
	global $db, $db_config, $global_config, $module_config, $module_name, $array_block_cat_module;
	//$bodyhtml = stripwhitespace($bodyhtml);
	$rowcontent = array(
		"id" => "",
		"topicid" => "",
		"admin_id" => 1,
		"author" => "",
		"sourceid" => 0,
		"addtime" => NV_CURRENTTIME,
		"edittime" => NV_CURRENTTIME,
		"status" => 0,
		"publtime" => NV_CURRENTTIME,
		"exptime" => 0,
		"archive" => 1,
		"title" => "",
		"alias" => "",
		"hometext" => "",
		"sourcetext" => "",
		"homeimgfile" => "",
		"homeimgalt" => "",
		"homeimgthumb" => "",
		"imgposition" => 1,
		"bodytext" => "",
		"copyright" => 0,
		"inhome" => 1,
		"allowed_comm" => 2,
		"allowed_rating" => 1,
		"allowed_send" => 1,
		"allowed_print" => 1,
		"allowed_save" => 1,
		"hitstotal" => 0,
		"hitscm" => 0,
		"total_rating" => 0,
		"click_rating" => 0,
		"keywords" => "" );
	if( $rowcontent['keywords'] == "" )
	{
		if( $rowcontent['hometext'] != "" )
		{
			$rowcontent['keywords'] = nv_get_keywords( $hometext );
		}
		else
		{
			$rowcontent['keywords'] = nv_get_keywords( nv_fil_tag( $bodyhtml ) );
		}
	}
	$mod_data = str_replace( '-', '_', $module );
	$hitstotal = rand( 1, 5 );
	$query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $mod_data . "_rows` 
				(`id`, `catid`, `listcatid`, `topicid`, `admin_id`, `author`, `sourceid`, `addtime`, `edittime`, `status`, `publtime`, `exptime`, `archive`, `title`, `alias`, `hometext`, `homeimgfile`, `homeimgalt`, `homeimgthumb`, `inhome`, `allowed_comm`, `allowed_rating`, `hitstotal`, `hitscm`, `total_rating`, `click_rating`, `keywords`) VALUES 
                (NULL, 
                " . intval( $catids ) . ",
                " . $db->dbescape_string( $catids ) . ",
                " . intval( 0 ) . ",
                " . intval( 1 ) . ",
                " . $db->dbescape_string( $rowcontent['author'] ) . ",
                " . intval( $sourceid ) . ",
                " . intval( $publtime ) . ",
                " . intval( $publtime ) . ",
                " . intval( $module_config[$module_name]['active'] ) . ",
                " . intval( $publtime ) . ",
                " . intval( 0 ) . ", 
                " . intval( 2 ) . ",
                " . $db->dbescape_string( $title ) . ",
                " . $db->dbescape_string( $alias ) . ",
                " . $db->dbescape_string( $hometext ) . ",
                " . $db->dbescape_string( $homefile ) . ",
                " . $db->dbescape_string( "" ) . ",
                " . $db->dbescape_string( $homeimgthumb ) . ",
                " . intval( 1 ) . ",  
                " . intval( 2 ) . ", 
                " . intval( 1 ) . ", 
                " . intval( $hitstotal ) . ",  
                " . intval( 0 ) . ",  
                " . intval( 0 ) . ",  
                " . intval( 0 ) . ",  
                " . $db->dbescape_string( $rowcontent['keywords'] ) . ")";
	$rowcontent['id'] = $db->sql_query_insert_id( $query );
	$error = array();
	if( $rowcontent['id'] > 0 )
	{
		$rowcontent['bodytext'] = nv_news_get_bodytext( $bodyhtml );
		$ct_query = array();
		$tbhtml = NV_PREFIXLANG . "_" . $mod_data . "_bodyhtml_" . ceil( $rowcontent['id'] / 2000 );
		$db->sql_query( "CREATE TABLE IF NOT EXISTS `" . $tbhtml . "` (`id` int(11) unsigned NOT NULL, `bodyhtml` longtext NOT NULL, `sourcetext` varchar(255) NOT NULL default '', `imgposition` tinyint(1) NOT NULL default '1', `copyright` tinyint(1) NOT NULL default '0', `allowed_send` tinyint(1) NOT NULL default '0', `allowed_print` tinyint(1) NOT NULL default '0', `allowed_save` tinyint(1) NOT NULL default '0', PRIMARY KEY  (`id`)) ENGINE=MyISAM" );
		$ct_query[] = ( int )$db->sql_query( "INSERT INTO `" . $tbhtml . "` VALUES 
					(" . $rowcontent['id'] . ", 
					" . $db->dbescape_string( $bodyhtml ) . ", 
	                " . $db->dbescape_string( $link ) . ",
					" . intval( $module_config[$module_name]['imgposition'] ) . ",
	                " . intval( 0 ) . ",  
	                " . intval( 1 ) . ",  
	                " . intval( 1 ) . ",  
	                " . intval( 1 ) . "					
					)" );

		$ct_query[] = ( int )$db->sql_query( "INSERT INTO `" . NV_PREFIXLANG . "_" . $mod_data . "_" . $catids . "` SELECT * FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_rows` WHERE `id`=" . $rowcontent['id'] . "" );

		$ct_query[] = ( int )$db->sql_query( "INSERT INTO `" . NV_PREFIXLANG . "_" . $mod_data . "_bodytext` VALUES (" . $rowcontent['id'] . ", " . $db->dbescape_string( $rowcontent['bodytext'] ) . ")" );
		if( array_sum( $ct_query ) != sizeof( $ct_query ) )
		{
			$error[] = "không thể lưu";
		}
		unset( $ct_query );
	}
	else
	{
		$error[] = "không thể lưu";
	}
	foreach( $id_block_content as $bid_i )
	{
		$db->sql_query( "INSERT INTO `" . NV_PREFIXLANG . "_" . $mod_data . "_block` (`bid`, `id`, `weight`) VALUES ('" . $bid_i . "', '" . $rowcontent['id'] . "', '0')" );
	}

	$id_block_content[] = 0;
	$db->sql_query( "DELETE FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_block` WHERE `id` = " . $rowcontent['id'] . " AND `bid` NOT IN (" . implode( ",", $id_block_content ) . ")" );
	$id_block_content = array_keys( $array_block_cat_module );
	foreach( $id_block_content as $bid_i )
	{
		nv_news_fix_block( $bid_i, false );
	}
	$db->sql_freeresult();
}

function nv_main_cron( $contents )
{
	global $module_config, $module_name, $lang_module, $db;
	$link = "" . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&" . NV_OP_VARIABLE . "=add";
	$return = "";
	$return .= "<form method=\"post\"><table class=\"tab1 getnews\">";
	$return .= "<thead>";
	$return .= "<tr>";
	$return .= "<td colspan=\"3\">";
	$return .= $lang_module['config'];
	$return .= "</td>";
	$return .= "</tr>";
	$return .= "</thead>";

	$return .= "<tbody><tr>";
	$return .= "<td width=200><strong>" . $lang_module['module'] . "</strong></td>";
	$return .= "<td align=center><select style='width:160px' name=\"module\">";
	$sql = "SELECT * FROM `" . NV_PREFIXLANG . "_modules` WHERE `module_file`='news'";
	$result = $db->sql_query( $sql );
	while( $rows = $db->sql_fetchrow( $result ) )
	{
		$select1 = "";
		if( $rows['module_data'] == $module_config[$module_name]['module'] )
		{
			$select1 = " selected=\"selected\"";
		}
		$return .= "<option " . $select1 . " value=\"" . $rows['module_data'] . "\">" . $rows['title'] . "</option>";
	}
	$return .= "</select>\n";
	$return .= "</td><td></td>";
	$return .= "</tr></tbody>";

	$return .= "<tbody><tr>";
	$return .= "<td width=200><strong>" . $lang_module['active'] . "</strong></td>";
	$return .= "<td align=center><select style='width:160px' name=\"active\">";
	$arr1 = array( 0 => "Chờ Duyệt", 1 => "Cho Đăng" );
	foreach( $arr1 as $num1 => $title1 )
	{
		$select1 = "";
		if( $num1 == $module_config[$module_name]['active'] )
		{
			$select1 = " selected=\"selected\"";
		}
		$return .= "<option " . $select1 . " value=\"" . $num1 . "\">" . $title1 . "</option>";
	}
	$return .= "</select>\n";
	$return .= "</td><td>" . $lang_module['note1'] . "</td>";
	$return .= "</tr></tbody>";

	$return .= "<tbody class=\"second\"><tr>";
	$return .= "<td><strong>" . $lang_module['load_image'] . "</strong></td>";
	$return .= "<td align=center><select style='width:160px' name=\"image_body\">";
	$arr = array( 'không', 'có' );
	foreach( $arr as $num => $title )
	{
		$select = "";
		if( $num == $module_config[$module_name]['image_body'] )
		{
			$select = " selected=\"selected\"";
		}
		$return .= "<option " . $select . " value=\"" . $num . "\">" . $title . "</option>";
	}
	$return .= "</select>\n";
	$return .= "</td><td width=400>" . $lang_module['note2'] . "</td>";
	$return .= "</tr></tbody>";

	$return .= "<tbody class=\"second\"><tr>";
	$return .= "<td><strong>" . $lang_module['size'] . "</strong></td>";
	$return .= "<td align=center>\n";
	$return .= "<input size=6 name=\"width\" value=\"" . $module_config[$module_name]['width'] . "\">X\n";
	$return .= "<input size=6 name=\"height\" value=\"" . $module_config[$module_name]['height'] . "\">\n";

	$return .= "</td><td width=400><strong>Click xem </strong><a href=\"javascript:void(0);\" onclick=\"window.open('http://www.youtube.com/v/IDvCXomfiYo','popup','width=800,height=600,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=300,top=0'); return false\"><strong>Video hướng dẫn<strong></strong></strong></a></td>";
	$return .= "</tr></tbody>";

	$return .= "<tbody><tr>";
	$return .= "<td><strong>" . $lang_module['time'] . "</strong></td>";
	$return .= "<td align=center>\n";
	$return .= "<a class=\"button1\" style=\"margin-left:145px\" href=\"javascript:void(0);\" onclick=\"update_time();\"><span><span>Cập nhật</span></span></a>\n";
	$return .= "</td><td width=400>&nbsp;</td>";
	$return .= "</tr></tbody>";

	$return .= "<tbody class=\"second\"><tr>";
	$return .= "<td><strong>" . $lang_module['img_ht'] . "</strong></td>";
	$return .= "<td align=center><select style='width:160px' name=\"imgposition\">";
	$arr = array(
		$lang_module['position0'],
		$lang_module['position1'],
		$lang_module['position2'] );
	foreach( $arr as $num => $title )
	{
		$select = "";
		if( $num == $module_config[$module_name]['imgposition'] )
		{
			$select = " selected=\"selected\"";
		}
		$return .= "<option " . $select . " value=\"" . $num . "\">" . $title . "</option>";
	}
	$return .= "</select>\n";
	$return .= "</td><td width=400></td>";
	$return .= "</tr></tbody>";

	$return .= "<tbody class=\"second\"><tr>";
	$return .= "<td><strong>" . $lang_module['cron'] . "</strong></td>";
	$return .= "<td align=center><select style='width:160px' name=\"select_options\" onchange=\"top.location.href=this.options[this.selectedIndex].value;return;\">";
	$return .= "<option value=\"\">" . $lang_module['add1'] . "</option>";
	$return .= "<option value=\"" . $link . "\">" . $lang_module['add'] . "</option>";
	$return .= "</select>\n";
	$return .= "</td><td width=400></td>";
	$return .= "</tr></tbody>";

	$return .= "<td colspan=\"1\"></td><td align=center >
					<input type=\"hidden\" name=\"do\" id=\"do\" value=\"1\">
					<input style=\"width:100px;\" name=\"save\" value=\"" . $lang_module['save'] . "\" type=\"submit\">
					</td><td></td>";
	$return .= "</table></form>\n";
	if( ! empty( $contents ) )
	{
		foreach( $contents as $id => $values )
		{
			$return .= "<table summary=\"" . $values['caption'] . "\" class=\"tab1\">\n";
			$return .= "<col span=\"2\" valign=\"top\" width=\"50%\" />\n";
			$return .= "<thead>\n";
			$return .= "<tr>\n";
			$return .= "<td colspan=\"2\">\n";
			$return .= "<div style=\"position:absolute;right:10px\">\n";
			if( ! empty( $values['edit'][0] ) )
			{
				$return .= "<a class=\"button1\" href=\"" . $values['edit'][2] . "\"><span><span>" . $values['edit'][1] . "</span></span></a>\n";
			}
			if( ! empty( $values['disable'][0] ) )
			{
				$return .= "<a class=\"button1\" href=\"" . $values['disable'][2] . "\"><span><span>" . $values['disable'][1] . "</span></span></a>\n";
			}
			if( ! empty( $values['delete'][0] ) )
			{
				$return .= "<a class=\"button1\" href=\"javascript:void(0);\" onclick=\"nv_is_del_cron(" . $id . ");\"><span><span>" . $values['delete'][1] . "</span></span></a>\n";
			}
			$return .= "</div>\n";
			$return .= $values['caption'] . "</td>\n";
			$return .= "</tr>\n";
			$return .= "</thead>\n";
			$a = 0;
			if( is_array( $values['detail'] ) )
			{
				foreach( $values['detail'] as $key => $value )
				{
					$class = ( $a % 2 ) ? " class=\"second\"" : "";
					$return .= "<tbody" . $class . ">\n";
					$return .= "<tr>\n";
					$return .= "<td>" . $key . "</td>\n";
					$return .= "<td>" . $value . "</td>\n";
					$return .= "</tr>\n";
					$return .= "</tbody>\n";
					$a++;
				}
				$return .= "</table>\n";
			}
		}
	}

	return $return;
}

?>